﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MedicalSystems
{
    public partial class AddRecord : Form
    {
        private List<string> Parameters;
        private List<string> units;
        private Form1 form;
        public AddRecord(Form1 f)
        {
            InitializeComponent();
            Parameters = new List<string>();
            units = new List<string>();
            form = f;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string from = Globals.CURRENT_DATE.ToString();
            string to = Globals.CURRENT_DATE.ToString();

            if (txtFirstName.Text=="" || txtLastName.Text == "" || txtValue.Text == "" || cmbParam.Text == "" || cmbUnit.Text == "")
            {
                MessageBox.Show("Please fill in all required fields", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                if (!cbFrom.Checked && cmbFrom.Text == "")
                {
                    MessageBox.Show("Please select start time", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    if (!cbTo.Checked && cmbTo.Text == "")
                    {
                        MessageBox.Show("Please select finish time", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        if (!cbFrom.Checked) from = dtFrom.Value.ToShortDateString() + " " + cmbFrom.SelectedItem.ToString();
                        if (!cbTo.Checked) to = dtTo.Value.ToShortDateString() + " " + cmbTo.SelectedItem.ToString();

                        if (Convert.ToDateTime(to) < Convert.ToDateTime(from))
                        {
                            MessageBox.Show("Finish time must not be earlier than start time", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        else
                        {
                            Globals.dataSet.Add(new Record(txtFirstName.Text, txtLastName.Text,cmbGender.Text,
                                                                                cmbParam.Text, txtValue.Text, cmbUnit.Text, from, to, Globals.CURRENT_DATE.ToString()));
                            Globals.setTxnFinish();
                            btnCancel.Text = "Close";
                            MessageBox.Show("Record successfully  added.", "Record added");
                            form.saveToolStripMenuItem_Click(sender, e);
                            form.refreshToolStripMenuItem_Click(sender, e);
                            form.saveToolStripMenuItem_Click(sender, e);
                        }
                    }
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmbFrom_SelectedIndexChanged(object sender, EventArgs e)
        {
            int ans = cmbFrom.SelectedIndex;
            if (ans != cmbFrom.Items.Count - 1) ans++;
            cmbTo.SelectedIndex = ans;
        }

        private void dtFrom_ValueChanged(object sender, EventArgs e)
        {
            dtTo.Value = dtFrom.Value;
        }

        private void AddRecord_Load(object sender, EventArgs e)
        {
            units = (from rec in Globals.dataSet
                     orderby rec.Unit
                     select rec.Unit).Distinct().ToList();
            Parameters = (from rec in Globals.dataSet
                          orderby rec.Parameter
                          select rec.Parameter).Distinct().ToList();

            cmbUnit.DataSource = units;
            cmbParam.DataSource = Parameters;
            cmbParam.SelectedIndex = 0;
            cmbUnit.SelectedIndex = 0;
            cmbFrom.SelectedIndex = 0;
            cmbTo.SelectedIndex = 0;

            dtFrom.Value = Globals.CURRENT_DATE;
            dtTo.Value = Globals.CURRENT_DATE;

            cmbGender.SelectedIndex = 0;
        }

        private void cbFrom_CheckedChanged(object sender, EventArgs e)
        {
            dtFrom.Enabled = !cbFrom.Checked;
            cmbFrom.Enabled = !cbFrom.Checked;
            if (cbFrom.Checked)
            {
                dtFrom.Value = Globals.CURRENT_DATE;
            }
        }

        private void cbTo_CheckedChanged(object sender, EventArgs e)
        {
            dtTo.Enabled = !cbTo.Checked;
            cmbTo.Enabled = !cbTo.Checked;
            if (cbTo.Checked)
            {
                dtTo.Value = Globals.CURRENT_DATE;
            }

        }
    }
}
